package tools.mybatis.plugin;

import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class SelectReturnIdPlug extends PluginAdapter {

	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {

		IntrospectedColumn column = introspectedTable.getColumn("id");
		if (column != null) {
			// 添加自动主键
			element.addAttribute(new Attribute("useGeneratedKeys", "true"));
			element.addAttribute(new Attribute("keyProperty", "id"));
		}
		return super.sqlMapInsertSelectiveElementGenerated(element, introspectedTable);
	}

	@Override
	public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
		IntrospectedColumn column = introspectedTable.getColumn("id");
		if (column != null) {
			// 添加自动主键
			element.addAttribute(new Attribute("useGeneratedKeys", "true"));
			element.addAttribute(new Attribute("keyProperty", "id"));
		}
		return super.sqlMapInsertElementGenerated(element, introspectedTable);
	}
}
